package com.project.controller;

import com.alibaba.fastjson.JSON;
import com.project.baseInfo.model.LoginUserInfo;
import com.project.constants.RedisConstants;
import com.project.constants.SessionConstants;
import com.project.baseInfo.model.Result;
import com.project.baseInfo.model.User;
import com.project.service.UserService;
import com.project.utils.MD5Utils;
import com.project.utils.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/login")
public class LoginController {
    private static Logger log=LoggerFactory.getLogger(UserController.class);
    @Resource(name = "userServiceImpl")
    private UserService userService;
    @Autowired
    private RedisCache redisCache;

    @RequestMapping(value = "/login.do", method = RequestMethod.GET)
    public String loginPage(HttpServletRequest req){
        return "../../index";
    }

    @ResponseBody
    @RequestMapping(value="/login.do",method=RequestMethod.POST)
    public Result<String> login(HttpServletRequest request, Model model, User user){
        Result<String> result = new Result<String>();
        if (null == user||StringUtils.isEmpty(user.getUserName())||StringUtils.isEmpty(user.getPassword())){
            result.setCode(Result.FAIL);
            result.setInfo("缺少入参");
            return result;
        }
        User userInfo = userService.getUserByUserName(user.getUserName(),0);
        if (null == userInfo||!userInfo.getPassword().equals(user.getPassword())){
            result.setCode(Result.FAIL);
            result.setInfo("用户名或密码不正确");
            return result;
        }
        String token;
        try {
            token = MD5Utils.getEncryption(userInfo.getId().toString()+userInfo.getUserName()+String.valueOf(System.currentTimeMillis()));
            LoginUserInfo loginUserInfo = new LoginUserInfo();
            loginUserInfo.setId(userInfo.getId());
            loginUserInfo.setPassword(userInfo.getPassword());
            loginUserInfo.setUserName(userInfo.getUserName());
            loginUserInfo.setToken(token);
            redisCache.putObject(token,JSON.toJSONString(loginUserInfo),RedisConstants.LOGIN_DURATION);
            redisCache.putObject(loginUserInfo.getId(),token,RedisConstants.LOGIN_DURATION);
            request.getSession().setAttribute(SessionConstants.USER, loginUserInfo);
        }catch (Exception e){
            log.error("登录异常",e);
            result.setCode(Result.FAIL);
            result.setInfo("登录异常");
            return result;
        }

        result.setCode(Result.SUCCESS);
        result.setData(token);
        return result;
    }

    @RequestMapping(value="/welcome.do",method=RequestMethod.GET)
    public String welcome(HttpServletRequest request, Model model){
        // 从sesion中获得用户数据返回到页面
        LoginUserInfo loginUserInfo = (LoginUserInfo) request.getSession().getAttribute(SessionConstants.USER);
        model.addAttribute("user", loginUserInfo);
        return "welcome";
    }

    @RequestMapping(value = "/jumpLogin.do", method = RequestMethod.GET)
    public String jumpLogin(HttpServletRequest req){
        return "jumpLogin";
    }
}
